/**
 * Created by ljy on 2016/6/23.
 */

define(function(require,exports,module){
    //年倒计时
    setInterval(function () {
        var date = new Date()
        var nowYear = date.getFullYear()
        date.setFullYear(nowYear + 1,0,1)
        date.setHours(0,0,0,0)
        var desTime = date.valueOf()
        var nowTime = new Date()
        var time = desTime - nowTime
        var days = Math.floor(time / (1000 * 60 * 60 * 24))
        var hours = time % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)
        var hours = Math.floor(hours)
        var minutes = (time % (1000 * 60 * 60)) / (1000 * 60)
        var minutes = Math.floor(minutes)
        var seconds = (time % (1000 * 60)) / 1000
        var seconds = Math.floor(seconds)
        var span = document.getElementById("timer")
        span.innerHTML = "<p>" + nowYear + "</p>" + "年还剩" +days +"天" +hours+ "小时"+minutes+ "分钟"+seconds+"秒"
    },1000)


    //ajax实时天气的代码

    var span = document.getElementById("time");

    function getWeather() {
        //基于jQuery的实时天气功能实现
        $.ajax({
            //设置请求类型
            type:"get",
            //
            timeout:5000,
            //设置请求地址
            url: 'http://api.map.baidu.com/telematics/v3/weather',
            //设置需要发送的数据
            data: {location: "北京", output: 'json', ak: '0A5bc3c4fb543c8f9bc54b77bc155724'},
            //定义此请求为跨域请求
            dataType: 'jsonp',
            success: function (data) {
                console.log(data)
                //请求成功执行的代码
                //获取到与功能相关的数据
                var today =  data['results'][0]['weather_data'][0];
                //把请求到的数据渲染在页面上
                span.innerText = today.date;
                console.log()
                $("#daypic").html("<img src="+ today.dayPictureUrl +" >");
                $("#nigpic").html("<img src="+ today.nightPictureUrl +">");
                $("#wea").html(today.weather);

            },
            error: function () {
                span.innerText = "天气请求超时";
                $("#daypic").html("");
                $("#nigpic").html("");
                $("#wea").html("");
            }
        })
    }
    getWeather();

    //连接网络时，获取天气
    window.ononline = function () {
        getWeather();
    }

    //无网络连接时，提醒用户联网以获取天气
    window.onoffline = function () {

        span.innerText = "请连接网络以获取天气";
    }



})

